import {defineConfig, loadEnv} from 'vite';
import vue from '@vitejs/plugin-vue';
import {resolve} from 'path';
import Components from 'unplugin-vue-components/vite';
import {VantResolver} from 'unplugin-vue-components/resolvers';
import {manualChunksPlugin} from 'vite-plugin-webpackchunkname';
import viteCompression from 'vite-plugin-compression';
import brotli from 'rollup-plugin-brotli';
// https://vitejs.dev/config/
const reg = /(.*)\/src\/components\/(.*)/;
const cookie =
    'UUAP_P_TOKEN=PT-857189603768541185-TOlZjutzEQ-uuap; SECURE_UUAP_P_TOKEN=PT-857189603768541185-TOlZjutzEQ-uuap; UUAP_TRACE_TOKEN=0338350554bc549d93938f8b731e8ed1; jsdk-uuid=5d2b105f-c5c4-4c80-ac83-bc0b7ffaa0fb; jsdk-user=fOOa2h4ipefs3KanG2z9Lg==; __bid_n=184e18fb25063afba54207; FPTOKEN=GuKBffhTRvEizwqPdVz5KrGdcsW6i5hpxv4X1YZFUwZkKOHRKsnU+CZcyVNxe4Ef4SBJ+fibj+iSbTKAhKMnA/B5+7Pr7wKdqmmWxqndeamewUF0gnr2ZU2Jb9SFxJ9TR1LWKMLnbfuW0jgosJBnsXdaljgDagDRgDJY6udNf51wIJ8oQnUTeU2gki5DNVz5bVu6YMT4ttR0Mw4LyTlr1vIQzklGVtWzk+hzT1mQM98MLcM6LmRbqT7nzAnErHL34wWPWFWwJFjCUCTII3cAsbyP1BnaEmyKtcgvFiD6xKqWb0abR/eCxze6r4RJpjBc60fqLM/A7TI0Az8+6aRAEPzjZY8gfHTwBepCkL6z6QjAIWzfeT8AKimH230LmZyDw1G/bdYSIhM4oUVCv4yEOA==|4FI4boHZq8Bx+8o7wqzektrihZkG/fD9gvUGiqdg7g0=|10|963d04d21922c05c1f26d1973d373e7c; UUAP_P_TOKEN_OFFLINE=PT-857212220885315585-nyOXUHTpvp-beta; SECURE_UUAP_P_TOKEN_OFFLINE=PT-857212220885315585-nyOXUHTpvp-beta; UUAP_TRACE_TOKEN_OFFLINE=0338350554bc549d93938f8b731e8ed1; INSTANCE_HASH=b078783c42359c1a69646f6f28b35f1c; SERVER_ENV=A; MCITY=-131%3A; BIDUPSID=D3EBFF40F578E9C11F325113745C24E5; PSTM=1683284558; BAIDUID=4FC92E45C2A52CC8B42C343CF320E9B0:FG=1; ZFY=4:B3TQ:AjveMq0p1:A0iD4DRlH0:BbhQC0dpFYREY09QWN8:C; BAIDUID_BFESS=4FC92E45C2A52CC8B42C343CF320E9B0:FG=1; newlogin=1; ZD_ENTRY=google; UUAP_S_TOKEN=ST-867402765981372416-siv1L-beta; H_PS_PSSID=38516_36559_38618_38592_38595_38580_38485_38637_26350_38621_38681; BA_HECTOR=al250h002k202h810h8h803l1i6m2eu1m; delPer=0; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; PSINO=1; ab_sr=1.0.1_MWMzYjY3YjUxMTBkYzMxMTYzOGI4MDFjYzZiNTkyMDJmMDJmNzA3NWRjMzZmODI1OWFhMmE5Mzk0NDBlNmY5YjQ3YTQyYWYwZWVlNjdlMDc0MGYzYjIwMTViOTAwNTM4OTQ3OGUxZjY3ZmJhYjRkOGYxNTAyY2U1ZDg0Y2VlOGNlNjBiNzE5ZjAwMjZmZjcyMzJiZTcyYTZkYWVmNWM4OQ==; ariaDefaultTheme=undefined; wmtoken=jPANDax8FunlJ30R7hsGNPr6yRlgD1XBJZxn8xIzupvKnOBtQ1ooJAkTbLTMYg1BavayLxjYwSuDFWEfXTD3MF7hXTxcVSk1sPLoz14oUvlobz21vh8Hd5OXwxy3Kb1NZrp78cKQ1BsBTaI2coUooA%3D%3D; RT="z=1&dm=baidu.com&si=fece9380-45a5-45dc-a57f-b14ccbf0b469&ss=lhyj5xz9&sl=4&tt=4kq&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf"';
export default defineConfig(({mode}) => {
    const env = loadEnv(mode, './env');
    console.log('环境-----', env);
    return {
        base: './',
        // publicDir: 'tess',
        build: {
            rollupOptions: {
                // manualChunks: {
                //     'dawei-test': [
                //         './src/views/backup/test.vue',
                //         './src/views/backup/test2.vue',
                //         './src/views/backup/test3.vue'
                //     ]
                // }
                output: {
                    manualChunks(id, {getModuleIds, getModuleInfo}) {
                        // 打包依赖
                        if (id.includes('node_modules')) {
                            // console.log(id.toString().split('node_modules/')[1].split('/')[1]);
                            //我这里用到其他包不多，最大的就是element-plus我把它单独拿出来，其余的都返回随便一个名字，
                            // 和之前没有配置相比，文件就只会多了element-plus和一个wq的js
                            if (id.toString().split('node_modules/')[1].split('/')[1].includes('element-plus')) {
                                return 'element-plus';
                            } else {
                                return 'vendor';
                            }
                        }
                        const importersLen = getModuleInfo(id).importers;
                        console.log(id, importersLen.length);
                        // 被多处引用
                        if (importersLen.length > 1) {
                            return 'common';
                        }
                        if (id.includes('src/views/backup/')) {
                            return 'dawei-test';
                        }
                    }
                }
            }
        },
        plugins: [
            vue(),
            Components({
                resolvers: [VantResolver()]
            }),
            // viteCompression()
            brotli()
            // manualChunksPlugin()
        ],
        server: {
            proxy: {
                '/api': {
                    target: 'http://127.0.0.1:8000', // 后端接口
                    changeOrigin: true,
                    headers: {
                        Cookie: cookie
                    },
                    rewrite: (path) => path.replace(/^\/api/, '')
                }
            }
        },
        resolve: {
            alias: {
                // @ 替代为 src
                '@': resolve(__dirname, 'src'),
                // @component 替代为 src/component
                '@components': resolve(__dirname, 'src/components')
            }
        }
    };
});
